package com.fortune.service.impl;


import com.fortune.dao.CProductWholesalePriceDao;
import com.fortune.dto.in.cPriceMgt.*;
import com.fortune.service.CProductWholesalePriceService;
import com.fortune.vo.cPriceMgt.CClientPrice;
import com.fortune.vo.cPriceMgt.CClientWholesalePrice;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CProductWholesalePriceServiceImpl implements CProductWholesalePriceService {
    @Autowired
    private CProductWholesalePriceDao cProductWholesalePriceDao;

    /**
     * 客户批发价列表
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public List<CClientPrice> queryProductPriceList(String dealerId, CPriceListDto listDto) {
        return cProductWholesalePriceDao.queryProductPriceList(dealerId,listDto);
    }

    /**
     * 客户批发价列表个数
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public int queryProductPriceListCount(String dealerId, CPriceListDto listDto) {
        return cProductWholesalePriceDao.queryProductPriceListCount(dealerId,listDto);
    }

    /**
     * 编辑客户批发
     * @param dealerId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editProductWholesalePrice(String dealerId, CPriceEditDto editDto) {
        int num = cProductWholesalePriceDao.queryIsExist(dealerId,editDto);
        if (num > 0){
            num = cProductWholesalePriceDao.editProductWholesalePrice(dealerId,editDto);
            if (num < 1){
                throw new RuntimeException("编辑客户批发失败");
            }
        }else {
            num = cProductWholesalePriceDao.addProductWholesalePrice(dealerId,editDto);
            if (num < 1){
                throw new RuntimeException("编辑客户批发失败");
            }
        }
        return 1;
    }


    /**
     * 查询客户统一批发价
     * @param dealerId
     * @param cWholesalePriceDto
     * @return
     */
    @Override
    public List<CClientWholesalePrice> queryProductWholesalePriceList(String dealerId, CWholesalePriceDto cWholesalePriceDto) {
        return cProductWholesalePriceDao.queryProductWholesalePriceList(dealerId,cWholesalePriceDto);
    }

    /**
     * 查询客户统一批发价个数
     * @param dealerId
     * @param cWholesalePriceDto
     * @return
     */
    @Override
    public int queryProductWholesalePriceListCount(String dealerId, CWholesalePriceDto cWholesalePriceDto) {
        return cProductWholesalePriceDao.queryProductWholesalePriceListCount(dealerId,cWholesalePriceDto);
    }

    /**
     * 编辑对客户统一批发价
     * @param dealerId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editDealerWholesalePrice(String dealerId, CWholePriceEditDto editDto) {
        int num = cProductWholesalePriceDao.queryWholeIsExist(dealerId,editDto);
        if (num < 1){
            num = cProductWholesalePriceDao.addDealerWholesalePrice(dealerId,editDto);
            if (num < 1){
                throw new RuntimeException("编辑对客户统一批发价失败");
            }
        }else {
            num = cProductWholesalePriceDao.editDealerWholesalePrice(dealerId,editDto);
            if (num < 1){
                throw new RuntimeException("编辑对客户统一批发价失败");
            }
        }
        return 1;
    }
}
